# 1.为了统计学生人数，可以给Student类增加一个类属性，每创建一个实例，该属性自动增加
class Student(object):
    count = 0
    def __init__(self,name):
        self.__name = name
        self.__age = 18
        Student.count += 1

    # 该装饰器可使方法变为属性，此时外界只能访问，不能更改
    @property
    def name(self):
        return self.__name

    # 将上面变为属性的方法赋予可更改的功能
    @name.setter
    def name(self,name):
        if not isinstance(name,str):
            raise ValueError("名字必须是字符串")
        else:
            self.__name = name

print("当前学生个数:%d" % Student.count)
stu1 = Student("zhangsan")
print("当前学生个数:%d" % Student.count)
stu2 = Student("lisi")
print("当前学生个数:%d" % Student.count)
print("stu1.name:%s" % stu1.name)
stu1.name = "wangwu"
print("stu1.name:%s" % stu1.name)

# 2.设计一个装饰器，它可以作用于任何函数上，打印函数执行时间
import time
def runtime(func):
    def wrapper(*args,**kwargs):
        start_time = time.time()
        result = func(*args,**kwargs)
        end_time = time.time()
        print("%s 执行耗时:%f秒" % (func.__name__,end_time - start_time))
    return wrapper

@runtime
def testfunc():
    mylist = [x * x for x in range(100000)]

# m = runtime(testfunc)
# m()
testfunc()